﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Setting_add_role : SessionPage
{
	LogicLayer mgr = new LogicLayer();

	protected SYS_Role curRole;
	protected string tip = "";
	protected string nodeArrayString = string.Empty;
	protected string curRoleID = string.Empty; 

    protected void Page_Load(object sender, EventArgs e)
    {
		curRoleID = Request.QueryString["id"] + "";

		if(!string.IsNullOrEmpty(curRoleID))
			curRole = mgr.GetSYS_Role(curRoleID);
		if(curRole == null)
			curRole = new SYS_Role();

		if (IsPostBack)
		{
			if (string.IsNullOrEmpty(curRoleID))
			{
				SYS_Role role = new SYS_Role();
				role.RoleID = Guid.NewGuid().ToString();
				role.Name = Request.Form["roleName"];
				role.Description = Request.Form["descr"];

				if (mgr.AddSYS_Role(role))
				{
					//添加function
					string funcStr = Request.Form["hidSelFuncId"];
					if (!string.IsNullOrEmpty(funcStr))
					{
						string[] fidArray = funcStr.Split(',');
						bool r = true;
						foreach (string fid in fidArray)
						{
							if (string.IsNullOrEmpty(fid))
								continue;

							SYS_RoleFunction rf = new SYS_RoleFunction();
							rf.FunctionID = fid;
							rf.RoleID = role.RoleID;
							r &= mgr.AddSYS_RoleFunction(rf);
						}
						if (!r)
							tip = "保存失败!";
						else
							Response.Write("<script>window.opener.location.reload();window.close();</script>");
					}
				}
				else
					tip = "保存失败!";
			}
			else
			{
				SYS_Role role = mgr.GetSYS_Role(curRoleID);
				role.Name = Request.Form["roleName"];
				role.Description = Request.Form["descr"];

				if (mgr.UpdateSYS_Role(role))
				{
					//delete function first
					mgr.DelRoleFunctions(curRoleID);

					//添加function
					string funcStr = Request.Form["hidSelFuncId"];
					if (!string.IsNullOrEmpty(funcStr))
					{						
						string[] fidArray = funcStr.Split(',');
						bool r = true;
						foreach (string fid in fidArray)
						{
							if (string.IsNullOrEmpty(fid))
								continue;

							SYS_RoleFunction rf = new SYS_RoleFunction();
							rf.FunctionID = fid;
							rf.RoleID = role.RoleID;
							r &= mgr.AddSYS_RoleFunction(rf);
						}
						if (!r)
							tip = "保存失败!";
						else
							Response.Write("<script>window.opener.location.reload();window.close();</script>");
					}
				}
				else
					tip = "保存失败!";
			}
		}

		nodeArrayString = GetNodeArrayString();
    }

	private string GetNodeArrayString()
	{
		try
		{
			/*{title: "Folder 2", isFolder: true,
						children: [
							{title: "Sub-item 2.1"},
							{title: "Sub-item 2.2"}
						]
					},
			 */
			string result = string.Empty;

			List<SYS_Function> funcList = mgr.ListFunctions();

			List<string> fidList = mgr.ListFunctionID(curRoleID);
			if (fidList == null)
				fidList = new List<string>();

			if (funcList != null)
			{
				string lastTag = "";
				bool sel = false;
				foreach (SYS_Function func in funcList)
				{
					sel = fidList.Contains(func.FunctionID);

					if (lastTag != func.Tag)
					{
						lastTag = func.Tag;

						result += "]},{ title: \"" + func.Tag + "\", key: \"ISFOLDER\", isFolder:true, hideCheckbox:true, expand:true , children: [";


						result += "{ title: \"" + func.Name + "\", key: \"" + func.FunctionID + "\"";
						if (sel)
							result += ", select:true";
						result += "}";
					}
					else
					{
						result += ",{ title: \"" + func.Name + "\", key: \"" + func.FunctionID + "\" ";
						if (sel)
							result += ", select:true";
						result += "}";
					}
				}

				if (!string.IsNullOrEmpty(result))
					result = result.Substring(3) + "]}";
			}
			return result;
		}
		catch
		{
			return string.Empty;
		}
	}
}